﻿/*---------------------------------------------------------------------------------
-----------------***DaLianQianYi***-----------------------
-----------------作者：大连仟亿科技------------------------------------------------------
-----------------联系作者：www.qianyikeji.com----------------------------------
-----------------时间：2024/7/26 11:30:36------------------------------------------
----------------------------------------------------------------------------------*/


using System;
using System.Text;
using System.Data;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Donal.Xms.Common;

namespace Donal.Xms.Web.qianyi.common
{
    public partial class products_list : Web.UI.ManagePage
    {
        protected int totalCount;
        protected int page;
        protected int pageSize;

        protected int category;
        protected int status;
        protected string property;
        protected string keywords = string.Empty;
        protected string prolistview = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            this.keywords = DTRequest.GetQueryString("keywords");
            this.category = DTRequest.GetQueryInt("category");
            this.property = DTRequest.GetQueryString("property");
            this.status = DTRequest.GetQueryInt("status");
            this.prolistview = Utils.GetCookie("products_list_view"); //显示方式  
            this.pageSize = GetPageSize(10); //每页数量
            if (!Page.IsPostBack)
            {
                ChkAdminLevel("products", DTEnums.ActionEnum.View.ToString()); //检查权限
                TreeBind();
                RptBind("id>0" + CombSqlTxt(keywords), "sort_id asc,id desc");
            }
        }

        #region 绑定类别=================================
        private void TreeBind()
        {
            BLL.products_category bll = new BLL.products_category();
            DataTable dt = bll.GetList(0);

            this.ddlCategory.Items.Clear();
            this.ddlCategory.Items.Add(new ListItem("所有类别", ""));

            foreach (DataRow dr in dt.Rows)
            {
                string Id = dr["id"].ToString();
                int ClassLayer = int.Parse(dr["class_layer"].ToString());
                string Title = dr["title"].ToString().Trim();

                if (ClassLayer == 1)
                {
                    this.ddlCategory.Items.Add(new ListItem(Title, Id));
                }
                else
                {
                    Title = "├ " + Title;
                    Title = Utils.StringOfChar(ClassLayer - 1, "　") + Title;
                    this.ddlCategory.Items.Add(new ListItem(Title, Id));
                }
            }
        }
        #endregion

        #region 数据绑定=================================
        private void RptBind(string _strWhere, string _orderby)
        {
            this.page = DTRequest.GetQueryInt("page", 1);

            this.ddlCategory.SelectedValue = category.ToString();
            this.ddlStatus.SelectedValue = this.status.ToString();
            this.ddlProperty.SelectedValue = this.property;
            this.txtKeywords.Text = this.keywords;

            //图表或列表显示
            BLL.products bll = new BLL.products();
            switch (this.prolistview)
            {
                case "Txt":
                    this.rptList2.Visible = false;
                    this.rptList1.DataSource = bll.GetList(this.pageSize, this.page, _strWhere, _orderby, out this.totalCount);
                    this.rptList1.DataBind();
                    break;
                default:
                    this.rptList1.Visible = false;
                    this.rptList2.DataSource = bll.GetList(this.pageSize, this.page, _strWhere, _orderby, out this.totalCount);
                    this.rptList2.DataBind();
                    break;
            }
            //绑定页码
            txtPageNum.Text = this.pageSize.ToString();
            string pageUrl = Utils.CombUrlTxt("products_list.aspx", "status={0}&category={1}&keywords={2}&property={3}&page={4}",
               this.status.ToString(), this.category.ToString(), this.keywords, this.property, "__id__");
            PageContent.InnerHtml = Utils.OutPageList(this.pageSize, this.page, this.totalCount, pageUrl, 8);
        }
        #endregion

        #region 组合SQL查询语句==========================
        protected string CombSqlTxt(string _keywords)
        {
            StringBuilder strTemp = new StringBuilder();
            _keywords = _keywords.Replace("'", "");
            if (!string.IsNullOrEmpty(_keywords))
            {
                strTemp.Append(" and (title like  '%" + _keywords
                    + "%' or sub_title like '%" + _keywords
                    + "%' or accuracy_level like '%" + _keywords
                    + "%' or fluid_temperature like '%" + _keywords
                    + "%' or flow_field_sensitivity_level like '%" + _keywords
                    + "%' or climate_and_mechanical_environmental_safety like '%" + _keywords
                    + "%' or electromagnetic_compatibility_level like '%" + _keywords
                    + "%' or power_waste like '%" + _keywords
                    + "%' or output_signal like '%" + _keywords
                    + "%' or communication_interface like '%" + _keywords
                    + "%' or power_supply like '%" + _keywords
                    + "%' or work_environment like '%" + _keywords
                    + "%' or pressure_loss like '%" + _keywords
                    + "%' or data_storage like '%" + _keywords
                    + "%' or execution_standards like '%" + _keywords
                    + "%' or abstracts like '%" + _keywords
                    + "%' or contents like '%" + _keywords + "%')");
            }

            switch (status)
            {
                case 1:
                    strTemp.Append(" and status=0");
                    break;
                case 2:
                    strTemp.Append(" and status=1");
                    break;
            }

            switch (property)
            {
                case "isMsg":
                    strTemp.Append(" and is_msg=1");
                    break;
                case "isTop":
                    strTemp.Append(" and is_top=1");
                    break;
                case "isRed":
                    strTemp.Append(" and is_red=1");
                    break;
                case "isHot":
                    strTemp.Append(" and is_hot=1");
                    break;
                case "isSlide":
                    strTemp.Append(" and is_slide=1");
                    break;
            }

            if (category > 0)
            {
                strTemp.Append(" and category_id in(select id from dt_products_category where class_list like '%," + category + ",%')");
            }

            return strTemp.ToString();
        }
        #endregion

        #region 返回每页数量=============================
        private int GetPageSize(int _default_size)
        {
            int _pagesize;
            if (int.TryParse(Utils.GetCookie("products_page_size", "Donal.XmsPage"), out _pagesize))
            {
                if (_pagesize > 0)
                {
                    return _pagesize;
                }
            }
            return _default_size;
        }
        #endregion

        //设置操作
        protected void rptList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            int id = Convert.ToInt32(((HiddenField)e.Item.FindControl("hidId")).Value);
            BLL.products bll = new BLL.products();
            Model.products model = bll.GetModel(id);
            switch (e.CommandName)
            {
                case "lbtnIsTop":
                    if (model.is_top == 1)
                        bll.UpdateField(id, "is_top=0");
                    else
                        bll.UpdateField(id, "is_top=1");
                    break;
                case "lbtnIsRed":
                    if (model.is_red == 1)
                        bll.UpdateField(id, "is_red=0");
                    else
                        bll.UpdateField(id, "is_red=1");
                    break;
                case "lbtnIsHot":
                    if (model.is_hot == 1)
                        bll.UpdateField(id, "is_hot=0");
                    else
                        bll.UpdateField(id, "is_hot=1");
                    break;
            }
            Redirect();
        }


        //设置文字列表显示
        protected void lbtnViewTxt_Click(object sender, EventArgs e)
        {
            Utils.WriteCookie("products_list_view", "Txt", 14400);
            Redirect();
        }

        //设置图文列表显示
        protected void lbtnViewImg_Click(object sender, EventArgs e)
        {
            Utils.WriteCookie("products_list_view", "Img", 14400);
            Redirect();
        }

        protected void Redirect()
        {
            Response.Redirect(Utils.CombUrlTxt("products_list.aspx", "status={0}&category={1}&keywords={2}&property={3}",
                ddlStatus.SelectedValue, ddlCategory.SelectedValue, txtKeywords.Text.Trim(), ddlProperty.SelectedValue));
        }

        //关健字查询
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            Redirect();
        }

        //设置分页数量
        protected void txtPageNum_TextChanged(object sender, EventArgs e)
        {
            int _pagesize;
            if (int.TryParse(txtPageNum.Text.Trim(), out _pagesize))
            {
                if (_pagesize > 0)
                {
                    Utils.WriteCookie("products_page_size", "Donal.XmsPage", _pagesize.ToString(), 14400);
                }
            }
            Response.Redirect(Utils.CombUrlTxt("products_list.aspx", "keywords={0}", this.keywords));
        }

        //批量删除
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            ChkAdminLevel("products", DTEnums.ActionEnum.Delete.ToString()); //检查权限
            int sucCount = 0; //成功数量
            int errorCount = 0; //失败数量
            BLL.products bll = new BLL.products();
            Repeater rptList = new Repeater();
            switch (this.prolistview)
            {
                case "Txt":
                    rptList = this.rptList1;
                    break;
                default:
                    rptList = this.rptList2;
                    break;
            }
            for (int i = 0; i < rptList.Items.Count; i++)
            {
                int id = Convert.ToInt32(((HiddenField)rptList.Items[i].FindControl("hidId")).Value);
                CheckBox cb = (CheckBox)rptList.Items[i].FindControl("chkId");
                if (cb.Checked)
                {
                    if (bll.Delete(id))
                    {
                        sucCount++;
                    }
                    else
                    {
                        errorCount++;
                    }
                }
            }
            AddAdminLog(DTEnums.ActionEnum.Edit.ToString(), "删除产品管理成功" + sucCount + "条，失败" + errorCount + "条"); //记录日志
            JscriptMsg("删除成功" + sucCount + "条，失败" + errorCount + "条！", Utils.CombUrlTxt("products_list.aspx", "status={0}&category={1}&keywords={2}&property={3}", this.status.ToString(), this.category.ToString(), this.keywords, this.property));
        }

        //保存排序
        protected void btnSave_Click(object sender, EventArgs e)
        {
            ChkAdminLevel("products", DTEnums.ActionEnum.Edit.ToString()); //检查权限
            BLL.products bll = new BLL.products();
            Repeater rptList = new Repeater();
            switch (this.prolistview)
            {
                case "Txt":
                    rptList = this.rptList1;
                    break;
                default:
                    rptList = this.rptList2;
                    break;
            }
            for (int i = 0; i < rptList.Items.Count; i++)
            {
                int id = Convert.ToInt32(((HiddenField)rptList.Items[i].FindControl("hidId")).Value);
                int sortId;
                if (!int.TryParse(((TextBox)rptList.Items[i].FindControl("txtSortId")).Text.Trim(), out sortId))
                {
                    sortId = 99;
                }
                bll.UpdateField(id, "sort_id=" + sortId.ToString());
            }
            AddAdminLog(DTEnums.ActionEnum.Edit.ToString(), "保存产品管理排序"); //记录日志
            JscriptMsg("保存排序成功！", Utils.CombUrlTxt("products_list.aspx", Utils.CombUrlTxt("products_list.aspx", "status={0}&category={1}&keywords={2}&property={3}", this.status.ToString(), this.category.ToString(), this.keywords, this.property)));
        }

        protected void ddlCategory_SelectedIndexChanged(object sender, EventArgs e)
        {
            Redirect();
        }

        protected void ddlType_SelectedIndexChanged(object sender, EventArgs e)
        {
            Redirect();
        }
    }
}

